package com.spj.controller;

import com.spj.entity.DUser;
import com.spj.entity.JsonMessage;
import com.spj.entity.User;
import com.spj.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * @author :spj
 * @Date:2022/3/31
 * @description:
 */
@Controller
@RequestMapping("/data")
public class DataTest {
    @ResponseBody
    @RequestMapping("registUser")
    public JsonMessage registUser(@RequestBody @Valid DUser user, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            FieldError fieldError = bindingResult.getFieldError();
            String defaultMessage = fieldError.getDefaultMessage();
            return new JsonMessage(400,"失败",defaultMessage);
        }
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("springConfig.xml");
        UserService userServiceImpl = applicationContext.getBean("userServiceImpl", UserService.class);
        userServiceImpl.registUser(user);
        return  new JsonMessage(200,"成功",user);
    }
}
